/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.mapmanagment;

import czStvDpds.logging.Logging;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.logging.Level;

/**
 *
 * @author divad
 */
public class TilesInfo {

    private static TilesInfo tilesInfo;
    private NamedTile[] tiles;

    public static TilesInfo getTilesInfo() {
        if (tilesInfo == null) {
            tilesInfo = new TilesInfo();
        }
        return tilesInfo;
    }

    private TilesInfo() {
        tiles = this.getSeparatedTiles(this.readLines(this.loadTilesInfo()));
    }

    private NamedTile separeteTile(String line) {
        int pozice = 0;
        int id = Integer.parseInt(line.substring(pozice, line.indexOf(';', pozice)));
        pozice = line.indexOf(';', pozice) + 1;
        boolean solid = Boolean.parseBoolean(line.substring(pozice, line.indexOf(';', pozice)));
        pozice = line.indexOf(';', pozice) + 1;
        boolean notBulletProof = Boolean.parseBoolean(line.substring(pozice, line.indexOf(';', pozice)));
        pozice = line.indexOf(';', pozice) + 1;
        boolean destroyable = Boolean.parseBoolean(line.substring(pozice, line.indexOf(';', pozice)));
        pozice = line.indexOf(';', pozice) + 1;
        boolean transparent = Boolean.parseBoolean(line.substring(pozice, line.indexOf(';', pozice)));
        pozice = line.indexOf(';', pozice) + 1;
        String name = line.substring(pozice, line.indexOf(';', pozice));
        return new NamedTile(id, solid, notBulletProof, destroyable, transparent, name);
    }

    private NamedTile[] getSeparatedTiles(String[] readedLines) {
        NamedTile[] tiles = new NamedTile[readedLines.length];
        for (int i = 0; i < tiles.length; i++) {
            tiles[i] = this.separeteTile(readedLines[i]);
        }
        return tiles;
    }

    private String[] readLines(String tilesInformations) {
        ArrayList<String> lines = new ArrayList(10);
        if (tilesInformations != null) {
            boolean hasNext = tilesInformations.indexOf('\n') > 0;
            int pozition = 0;
            while (hasNext) {
                String line = tilesInformations.substring(pozition, tilesInformations.indexOf('\n', pozition) - 1);
                if (line.substring(0, 2).compareTo("//") != 0) {
                    lines.add(line);
                }
                pozition = tilesInformations.indexOf('\n', pozition) + 1;
                hasNext = pozition < tilesInformations.length();
            }
        }
        return lines.toArray(new String[]{});
    }

    private String loadTilesInfo() {
        String tilesInformations = null;
        try {
            File f = new File("resources/mapTilesTypes.txt");
            FileInputStream fis = new FileInputStream(f);
            byte[] data = new byte[fis.available()];
            fis.read(data);
            tilesInformations = new String(data);
        } catch (Exception e) {
            Logging.getMainLogger().log(Level.WARNING, "Nelze nahrát tiles info - ", e);
            System.err.print(e);
        }
        return tilesInformations;
    }

    public Tile getTile(int id) {
        for (int i = 0; i < tiles.length; i++) {
            if (tiles[i].getId() == id) {
                return tiles[i].getTile();
            }
        }
        return null;
    }

    public Tile getTile(String name) {
        for (int i = 0; i < tiles.length; i++) {
            if (tiles[i].name.compareTo(name) == 0) {
                return tiles[i].getTile();
            }
        }
        return null;
    }

    public String getName(int id) {
        for (int i = 0; i < tiles.length; i++) {
            if (tiles[i].getId() == id) {
                return tiles[i].getName();
            }
        }
        return null;
    }

    public boolean isTransparent(int id) {
        for (int i = 0; i < tiles.length; i++) {
            if (tiles[i].getId() == id) {
                return tiles[i].isTransparent();
            }
        }
        return false;
    }

    public int[] getIDs() {
        int[] ids = new int[this.tiles.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = this.tiles[i].getId();
        }
        return ids;
    }

    public int getTilesNumber() {
        return this.tiles.length;
    }

    public Tile[] getTiles() {
        Tile[] tiles = new Tile[this.tiles.length];
        for (int index = 0; index < tiles.length; index++) {
            tiles[index] = this.tiles[index].getTile();
        }
        return tiles;
    }

    private class NamedTile {

        private Tile tile;
        private String name;
        boolean transparent;

        public NamedTile(int id, boolean solid, boolean allowsBulletsThrough, boolean destructible, boolean transparent, String name) {
            this.tile = new Tile(id, solid, allowsBulletsThrough, destructible);
            this.transparent = transparent;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public int getId() {
            return tile.getId();
        }

        public boolean isSolid() {
            return tile.isSolid();
        }

        public boolean isAllowsBulletsThrough() {
            return tile.isAllowsBulletsThrough();
        }

        public boolean isDestructible() {
            return tile.isDestructible();
        }

        public boolean isTransparent() {
            return transparent;
        }

        public Tile getTile() {
            return tile;
        }
    }
}
